package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2017/8/17 0017.
 */
@Controller
@RequestMapping(ControllerConstants.CUSER)
public class UserMController extends BaseMobileController {
    @Autowired
    private UserService userService;

    /**
     * 普通用户注册
     *
     * @param request
     * @param response
     */
    @RequestMapping("/register")
    public void register(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String phone = getString(json, "phone", "");
                String pwd = getString(json, "pwd", "");
                String code = getString(json, "code", "");
                return userService.registerConsumer(phone, pwd, code);
            }
        });
    }

    /**
     * 忘记密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/foreignPwd")
    public void foreignPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String userName = getString(json, "userName", "");
                String pwd = getString(json, "pwd", "");
                return userService.foreginPwd(userName, pwd);
            }
        });
    }

    /**
     * 个人信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getUserInfo")
    public void getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                return userService.getUserInfo(userId);
            }
        });
    }

    /**
     * 验证用户密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkUserPwd")
    public void checkUserPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                String pwd = getString(json, "pwd", "");
                return userService.checkUserPwd(userId, pwd);
            }
        });
    }

    /**
     * 修改用户密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateUserPwd")
    public void updateUserPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                String pwd = getString(json, "pwd", "");
                String surePwd = getString(json, "surePwd", "");
                return userService.updateUserPwd(userId, pwd, surePwd);
            }
        });
    }

    /**
     * 修改用户个人信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateUserInfo")
    public void updateUserInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "userId", 0);
                String sex = getString(json, "sex", "男");
                String headPic = getString(json, "headPic", "");
                String name = getString(json, "name", "");
                String hxKid = getString(json, "hxKid", "");
                return userService.updateUserInfo(id, headPic, name, hxKid, sex);
            }
        });
    }

    /**
     * 用户登录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String userName = getString(json, "phone", "");
                String pwd = getString(json, "password", "");
                int type = getInt(json, "type", 1);
                String code = getString(json, "code", "");
                return userService.login(userName, pwd, type, code);
            }
        });
    }

    /**
     * 换绑手机号
     */
    @RequestMapping("/changePhone")
    public void changePhone(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String userName = getString(json, "userName", "");
                int userId = getInt(json, "userId", 1);
                return userService.changePhone(userName, userId);
            }
        });
    }

    /**
     * 获取信息
     */
    @RequestMapping("/getUserInfoByTrendsUserId")
    public void getUserInfoByTrendsUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userId = getInt(json, "userId", 1);
                return userService.getUserInfoByTrendsUserId(userId);
            }
        });
    }

    /**
     * 养老保险认证（用户端个人信息认证）
     */
    @RequestMapping("/certification")
    public void certification(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userId = getInt(json, "userId", 1);
                String cardId = getString(json, "cardId", "");
                String cardName = getString(json, "cardName", "");
                String handheldPhotos = getString(json, "handheldPhotos", "");
                String fontCard = getString(json, "fontCard", "");
                String reverseCard = getString(json, "reverseCard", "");
                return userService.certification(userId, cardId, cardName, handheldPhotos, fontCard, reverseCard);
            }
        });
    }

    /**
     * 检查用户是否认证，并且认证是否通过
     */
    @RequestMapping("/checkIsCertification")
    public void checkIsCertification(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userId = getInt(json, "userId", 1);

                return userService.checkIsCertification(userId);
            }
        });
    }

}
